package cn.yuanqiao.web.controller.archive.management;

import cn.yuanqiao.archive.management.domain.YqZpgz;
import cn.yuanqiao.archive.management.domain.YqZpjl;
import cn.yuanqiao.archive.management.domain.YqZpmx;
import cn.yuanqiao.archive.management.service.IYqZpgzService;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 组盘规则Controller
 * 
 * @author sy
 * @date 2023-07-10
 */
@RestController
@RequestMapping("/archive/yqZpgz")
public class YqZpgzController extends BaseController
{
    @Autowired
    private IYqZpgzService yqZpgzService;

    /**
     * 查询组盘规则列表
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:list')")
    @GetMapping("/list")
    public TableDataInfo list(YqZpgz yqZpgz)
    {
        startPage();
        List<YqZpgz> list = yqZpgzService.selectYqZpgzList(yqZpgz);
        return getDataTable(list);
    }

    /**
     * 导出组盘规则列表
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:export')")
    @Log(title = "组盘规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqZpgz yqZpgz)
    {
        List<YqZpgz> list = yqZpgzService.selectYqZpgzList(yqZpgz);
        ExcelUtil<YqZpgz> util = new ExcelUtil<YqZpgz>(YqZpgz.class);
        util.exportExcel(response, list, "组盘规则数据");
    }

    /**
     * 获取组盘规则详细信息
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:query')")
    @GetMapping(value = "/{zpgzId}")
    public AjaxResult getInfo(@PathVariable("zpgzId") Long zpgzId)
    {
        return success(yqZpgzService.selectYqZpgzByZpgzId(zpgzId));
    }

    /**
     * 获取组盘规则详细信息根据盘规格ID
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:query')")
    @GetMapping(value = "/getZpgz/{pggId}")
    public AjaxResult getZpgzByPggId(@PathVariable("pggId") Long pggId)
    {
        return success(yqZpgzService.selectYqZpgzByPggId(pggId));
    }

    /**
     * 新增组盘规则
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:add')")
    @Log(title = "组盘规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqZpgz yqZpgz)
    {
        return success(yqZpgzService.insertYqZpgz(yqZpgz));
    }

    /**
     * 修改组盘规则
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:edit')")
    @Log(title = "组盘规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqZpgz yqZpgz)
    {
        return success(yqZpgzService.updateYqZpgz(yqZpgz));
    }

    /**
     * 删除组盘规则
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:remove')")
    @Log(title = "组盘规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zpgzIds}")
    public AjaxResult remove(@PathVariable Long[] zpgzIds)
    {
        return success(yqZpgzService.deleteYqZpgzByZpgzIds(zpgzIds));
    }

    /**
     * 根据组盘规则进行组盘
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:plan')")
    @Log(title = "组盘规则", businessType = BusinessType.SELECT)
    @PostMapping("/plan")
    public AjaxResult plan(HttpServletRequest request, @RequestBody YqZpgz yqZpgz)
    {
        String errorMsg = "";

        List<Map<YqZpjl,List<YqZpmx>>> list = yqZpgzService.plan(request,yqZpgz);
        for (Map.Entry<YqZpjl, List<YqZpmx>> entry : list.get(0).entrySet()) {
            YqZpjl key = entry.getKey();
            if (key != null) {
                errorMsg = key.getErrorMsg();
            }
        }
//        if (null!=yqZpgz.getState()&&0==yqZpgz.getState()){
//            if (null==yqZpgz.getZpgzId()){
//                yqZpgz.setZpgzId(SnowflakeIdGenerator.generateId());
//                errorMsg += yqZpgzService.insertYqZpgz(yqZpgz);
//            }else {
//                errorMsg += yqZpgzService.updateYqZpgz(yqZpgz);
//            }
//        }
        if (StringUtils.isNotEmpty(errorMsg)){
            return AjaxResult.error(errorMsg);
        }else {
            return AjaxResult.success(JSONObject.toJSON(list));
        }
//        return success(yqZpgzService.plan(request,yqZpgz));
    }

    /**
     * 新增组盘记录
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:addZpjl')")
    @Log(title = "组盘记录", businessType = BusinessType.INSERT)
    @GetMapping("/addZpjl/{pggId}")
    public AjaxResult addZpjl(HttpServletRequest request,@PathVariable("pggId") Long pggId)
    {
        return success(yqZpgzService.insertYqZpjl(request, pggId));
    }

    /**
     * 组盘下载
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:zpxz')")
    @Log(title = "组盘下载", businessType = BusinessType.INSERT)
    @PostMapping("/zpxz")
    public AjaxResult zpxz(HttpServletResponse response, Long zpjlId, Long ztId)
    {
        try {
            return success(yqZpgzService.zupan(response, zpjlId, ztId));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除组盘记录及组盘明细
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:remove')")
    @Log(title = "组盘规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeZpjl/{pggId}")
    public AjaxResult removeZpjl(HttpServletRequest request,@PathVariable("pggId") Long pggId)
    {
        return success(yqZpgzService.deleteYqZpjlByZpjlIds(request,pggId));
    }

    /**
     * 完成组盘
     */
//    @PreAuthorize("@ss.hasPermi('archive:yqZpgz:complete')")
    @Log(title = "完成组盘", businessType = BusinessType.INSERT)
    @GetMapping("/complete/{zpjlIds}")
    public AjaxResult complete(@PathVariable Long[] zpjlIds)
    {
        return success(yqZpgzService.complete(zpjlIds));
    }
}
